<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>角色管理</title>
<%@ include file="/common/header/meta.jsp"%>
<%@ include file="/common/header/script.jsp"%>
<script type="text/javascript">
	var $dg;
	$(function() {
		$dg = $("#dg").datagrid({
			url: base.contextPath+"/sysmgr/permits/datagrid.do",
			title: '<span style="font-weight:normal;color:#666;">系统基础配置-->机构用户-->权限组</span>', 
			idField: 'id', fit: true, border: false, pageSize: 30, pageList: [30,40,50,100,200], rownumbers: true,
			remoteSort: false, toolbar: '#toolbars', striped:true, pagination: true, singleSelect: true,
			frozenColumns: [[
			    { field: 'ck', checkbox: true },
			    { field: 'id', title: 'ID', width: 80, hidden: true }
			]],
			columns: [[
			    { field: 'name', title: '角色名称', width: 170, sortable: true },
			    { field: 'remark', title: '角色备注', width: 200, sortable: true }, 
			    { field: 'created', title: '创建时间', width: 140, sortable: true },
			    { field: 'modifyDate', title: '最后修改时间', width: 140, sortable: true },
			    { field: 'action', title: '操作', width: 375, formatter: function(value, row){
			    	var str = "" ;
			    	str += $.string.format("<a href='javascript:addMenus(\"{0}\");' class='clickLInk ext_sitemap'>添加菜单</a>", row.name) ;
			    	str += $.string.format("<a href='javascript:setpermits_User(\"{0}\");' class='clickLInk ext_user'>分配到用户</a>", row.name) ;
			    	str += $.string.format("<a href='javascript:setPermit_WorkGroup(\"{0}\");' class='clickLInk ext_customers'>分配到用户组</a>", row.name) ;
			    	str += $.string.format("<a href='javascript:setPermit_UserRole(\"{0}\");' class='clickLInk ext_customers'>分配到用户角色</a>", row.name) ;
			    	return str ;
			    }}
			]]
		}) ;
		
		var searchOpts = $("#topSearchbox").searchbox("options"), searcher = searchOpts.searcher;
        searchOpts.searcher = function (value, name) {
            if ($.isFunction(searcher)) { searcher.apply(this, arguments); }
            var o = {} ;
            o[name] = value ;
            $dg.datagrid("load",o);
        };
	});
	
	function form_edit(form) {
		var form_url = base.contextPath+"/sysmgr/permits/permits_form.do" ;
		if("E" == form) {
			var node = $dg.datagrid('getSelected');
			if (node) {
				form_url = base.contextPath+"/sysmgr/permits/permits_form.do?id="+node.id ;
			} else {
				alertify.warning("请选择一条记录！"); return ;
			}
		}
		var $d = $.easyui.showDialog({
			href: form_url, title: "表单", iniframe: false, topMost: true,
			width: 400, height: 200,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : '保存并继续', iconCls : 'icon-standard-disk', handler : function() { $.easyui.parent.submitForm($d, $dg, true) ; } },
              { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
	
	function del() {
		var rows = $dg.datagrid('getSelections');
		var ids = [] ;
		if (rows.length > 0) {
			$.messager.confirm("您确定要进行该操作？", function (c) { 
				if(c) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.easyui.loading({ msg: "数据删除中，请稍等..."});
					$.post(base.contextPath+"/sysmgr/permits/delete.do", {ids : ids.join(',')}, function(result) {
						if (result.status) {
							$dg.datagrid('clearSelections');$dg.datagrid('clearChecked');$dg.datagrid('reload') ;
							alertify.success(result.msg);
							$.easyui.loaded();
						} else {
							alertify.success(result.msg);
							$.easyui.loaded();
						}
					}, 'json').error(function() { $.easyui.loaded(); });
				}
			});
		} else {
			alertify.warning("请选择一条记录！");
			return false;
		}
	}
	
	function addMenus(name) {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			ids.join(',') ;
		} else {
			alertify.warning("请选择一条记录！");
			return ;
		}
		if(ids) {
			var $d = $.easyui.showDialog({
				href: base.contextPath+"/sysmgr/permits/permits_menu_page.do?id="+ids, 
				title: "添加资源["+name+"]", iniframe: false, topMost: true, width: 750, height: 430,
	            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	            buttons : [ 
	              { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
	              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
	           	]
	        });
		}
	}
	
	function setPermit_WorkGroup(name) {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			ids.join(',') ;
		} else {
			alertify.warning("请选择一条记录！");
			return ;
		}
		if(ids) {
			var $d = $.easyui.showDialog({
				href: base.contextPath+"/sysmgr/permits/permits_workgroup_page.do?id="+ids, 
				title: "角色分配用户组["+name+"]", iniframe: false, topMost: true, width: 750, height: 430,
	            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	            buttons : [ 
	              { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
	              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
	           	]
	        });
		}
	}
	
	function setPermit_UserRole(name) {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			ids.join(',') ;
		} else {
			alertify.warning("请选择一条记录！");
			return ;
		}
		if(ids) {
			var $d = $.easyui.showDialog({
				href: base.contextPath+"/sysmgr/permits/permits_userrole_page.do?id="+ids, 
				title: "角色分配用户组["+name+"]", iniframe: false, topMost: true, width: 750, height: 430,
	            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	            buttons : [ 
	              { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
	              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
	           	]
	        });
		}
	}
	
	function setpermits_User(name) {
		var rows = $dg.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			ids.join(',') ;
		} else {
			alertify.warning("请选择一条记录！");
			return ;
		}
		if(ids) {
			var $d = $.easyui.showDialog({
				href: base.contextPath+"/sysmgr/permits/permits_user_page.do?id="+ids, 
				title: "角色分配到用户["+name+"]", iniframe: false, topMost: true, width: 750, height: 430,
	            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	            buttons : [ 
	              { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $dg) ; } },
	              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
	           	]
	        });
		}
	}
	
</script>
</head>

<body>

	<div id="dg">
		<div id="toolbars">
            <a onClick="form_edit('A');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_add'">添加</a>
            <a onClick="form_edit('E');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_edit'">编辑</a>
            <a onClick="del();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_remove'">删除</a>
            <a onclick="$dg.datagrid('reload');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_reload'">刷新</a>
            <a onClick="$dg.datagrid('unselectAll');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-standard-application-xp'">取消选中</a>
            <input id="topSearchbox" class="easyui-searchbox" data-options="width: 250, height: 26, prompt: '请输入关键词', menu: '#topSearchboxMenu'" />
			<div id="topSearchboxMenu" style="width: 85px;">
				<div data-options="name:'name', iconCls: 'icon-hamburg-zoom'">角色名称</div>
			</div>
            <a onClick="$dg.datagrid('load',{});" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_clear'">取消筛选</a>
        </div>
	</div>
	

</body>
</html>